package cn.stronglink.assetmanage.business.setting.synctype;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.business.setting.synccontent.RecyclerViewSpacesItemDecoration;
import cn.stronglink.assetmanage.business.setting.synccontent.SyncListener;
import cn.stronglink.assetmanage.dao.DaoManager;
import cn.stronglink.assetmanage.entity.SyncTypes;

/**
 * A simple {@link Fragment} subclass.
 */
public class SystemSyncTypeFragment extends Fragment implements SyncListener {
    @BindView(R.id.recycleSyncType)
    RecyclerView recyclerView;
    private Unbinder unbinder;
    private SystemSyncTypeAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_system_sync_type,container,false);
        unbinder = ButterKnife.bind(this,view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        adapter = new SystemSyncTypeAdapter(getContext());
        adapter.setOnItemClickListener(this);
        HashMap<String, Integer> stringIntegerHashMap = new HashMap<>();
        stringIntegerHashMap.put(RecyclerViewSpacesItemDecoration.TOP_DECORATION, 50);//top间距

        stringIntegerHashMap.put(RecyclerViewSpacesItemDecoration.BOTTOM_DECORATION, 100);//底部间距

        stringIntegerHashMap.put(RecyclerViewSpacesItemDecoration.LEFT_DECORATION, 10);//左间距

        stringIntegerHashMap.put(RecyclerViewSpacesItemDecoration.RIGHT_DECORATION, 10);//右间距

        recyclerView.addItemDecoration(new RecyclerViewSpacesItemDecoration(stringIntegerHashMap));
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2, GridLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onSynItemClick(View view, int postion) {
        switch (postion) {
            case 0:
                if (!adapter.list.get(0).getIsSelected()){
                    SyncTypes entity = adapter.list.get(0);
                    entity.setIsSelected(true);
                    DaoManager.getDaoSession(getContext()).getSyncTypesDao().update(entity);
                    SyncTypes entity2 = adapter.list.get(1);
                    entity2.setIsSelected(false);
                    DaoManager.getDaoSession(getContext()).getSyncTypesDao().update(entity2);
                }
                else {
                    SyncTypes entity = adapter.list.get(0);
                    entity.setIsSelected(false);
                    DaoManager.getDaoSession(getContext()).getSyncTypesDao().update(entity);

                    SyncTypes entity2 = adapter.list.get(1);
                    entity2.setIsSelected(true);
                    DaoManager.getDaoSession(getContext()).getSyncTypesDao().update(entity2);
                }
                break;
            case 1:
                if (!adapter.list.get(1).getIsSelected()){
                    SyncTypes entity = adapter.list.get(1);
                    entity.setIsSelected(true);
                    DaoManager.getDaoSession(getContext()).getSyncTypesDao().update(entity);
                    SyncTypes entity2 = adapter.list.get(0);
                    entity2.setIsSelected(false);
                    DaoManager.getDaoSession(getContext()).getSyncTypesDao().update(entity2);
                }
                else {
                    SyncTypes entity = adapter.list.get(1);
                    entity.setIsSelected(false);
                    DaoManager.getDaoSession(getContext()).getSyncTypesDao().update(entity);

                    SyncTypes entity2 = adapter.list.get(0);
                    entity2.setIsSelected(true);
                    DaoManager.getDaoSession(getContext()).getSyncTypesDao().update(entity2);
                }
                break;
            default:
                break;
        }
        adapter.notifyDataSetChanged();
    }
}
